
// Open MoneySpinner Suite v1
// An open source business management software system written in Java and MySQL
// Recommended IDE is NetBeans IDE 7.0.1
// Support Web Site: http://www.milliscript.com
//
// Copyright (C) 2014, Abiodun Aremu, Ibadan/NIGERIA.
// Open MoneySpinner Suite is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
 * ViewLoginRecord.java
 *
 * Created on Jan 25, 2011, 1:05:37 AM
 */

package openmoneyspinnersuite;

/**
 *
 * @author Abiodun Aremu
 */
final class ViewLoginRecord extends javax.swing.JInternalFrame {

    Object[][] tableObject=new Object[][]{};
    Systems systems=new Systems();
    Thread processThread=new Thread();
    Thread titleThread=new Thread();
    /** Creates new form ViewLoginRecord */
    public ViewLoginRecord() {
        initComponents();
        loginRecordTable.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Login Code", "Employee Name", "Login Time", "Logout Time","Computer Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false,false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            refreshTable("");
            jRefreshButton.setActionCommand("r");
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginRecordScrollPane = new javax.swing.JScrollPane();
        loginRecordTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jRefreshButton = new javax.swing.JButton();
        jCancelButton = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMS_Finance_App.class).getContext().getResourceMap(ViewLoginRecord.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setToolTipText(resourceMap.getString("Form.toolTipText")); // NOI18N
        setFrameIcon(resourceMap.getIcon("Form.frameIcon")); // NOI18N
        setName("Form"); // NOI18N
        setVisible(true);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        loginRecordScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        loginRecordScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        loginRecordScrollPane.setName("loginRecordScrollPane"); // NOI18N

        loginRecordTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Login Code", "Employee Name", "Login Time", "Logout Time", "Computer Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        loginRecordTable.setName("loginRecordTable"); // NOI18N
        loginRecordScrollPane.setViewportView(loginRecordTable);
        loginRecordTable.getColumnModel().getColumn(0).setPreferredWidth(10);
        loginRecordTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("loginRecordTable.columnModel.title0")); // NOI18N
        loginRecordTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("loginRecordTable.columnModel.title1")); // NOI18N
        loginRecordTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("loginRecordTable.columnModel.title2")); // NOI18N
        loginRecordTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("loginRecordTable.columnModel.title3")); // NOI18N
        loginRecordTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("loginRecordTable.columnModel.title4")); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jRefreshButton.setFont(resourceMap.getFont("jRefreshButton.font")); // NOI18N
        jRefreshButton.setIcon(resourceMap.getIcon("jRefreshButton.icon")); // NOI18N
        jRefreshButton.setText(resourceMap.getString("jRefreshButton.text")); // NOI18N
        jRefreshButton.setToolTipText(resourceMap.getString("jRefreshButton.toolTipText")); // NOI18N
        jRefreshButton.setName("jRefreshButton"); // NOI18N
        jRefreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRefreshButtonActionPerformed(evt);
            }
        });

        jCancelButton.setFont(resourceMap.getFont("jCancelButton.font")); // NOI18N
        jCancelButton.setIcon(resourceMap.getIcon("jCancelButton.icon")); // NOI18N
        jCancelButton.setText(resourceMap.getString("jCancelButton.text")); // NOI18N
        jCancelButton.setName("jCancelButton"); // NOI18N
        jCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(492, Short.MAX_VALUE)
                .addComponent(jRefreshButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCancelButton)
                .addGap(20, 20, 20))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jCancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jRefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginRecordScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(loginRecordScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        // TODO add your handling code here:
        if(evt.isControlDown()){
              System.out.println("ctrl c");
        }
    }//GEN-LAST:event_formKeyPressed

    private void jRefreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefreshButtonActionPerformed
        
        /* Execute thread */
        processThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            refreshTable("");
            }
        });
        titleThread=new Thread(new Runnable() {
            public void run() {
                while(processThread.isAlive())setProcessingTitle(titleThread);
                enableControls();
                System.out.println("viewItemThread stopped runing.");
            }
        });
        processThread.start();
        titleThread.start();
    }//GEN-LAST:event_jRefreshButtonActionPerformed

    private void jCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelButtonActionPerformed
        
        if(processThread.isAlive())
        {
            try{
                processThread.stop();
                titleThread.stop();
                System.out.println("Threads successfully stopped.");
            }catch(Exception e){System.out.println("Thread stopping error: "+e);}
            this.setTitle("::. View System User Activity");
            enableControls();
        }
        else
        dispose();
}//GEN-LAST:event_jCancelButtonActionPerformed
    private void disableControls()
    {
        jRefreshButton.setEnabled(false);
    }                                             
    private void enableControls()
    {
        jRefreshButton.setEnabled(true);
        this.setTitle("::. View System User Activity");
    }
       
                  
    private void setProcessingTitle(Thread thread)
    {
        try{
            this.setTitle("::. View System User Activity - Processing Request");
            thread.sleep(500);
            this.setTitle("::. View System User Activity - Processing Request.");
            thread.sleep(500);
            this.setTitle("::. View System User Activity - Processing Request..");
            thread.sleep(500);
            this.setTitle("::. View System User Activity - Processing Request...");
            thread.sleep(500);
        }catch(Exception e){System.out.println("Title thread"+e);}
    }
    public void print()
    {
        try
        {
            loginRecordTable.print();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public void refreshTable(String searchPara)
    {
            String[] dbLoginCode=systems.getTableDataArrayNum(OpenMSApp.Database_A, "LoginRecord",2);
            String[] dbLoginTime=systems.getTableDataArrayNum(OpenMSApp.Database_A, "LoginRecord",3);
            String[] dbLogoutTime=systems.getTableDataArrayNum(OpenMSApp.Database_A, "LoginRecord",4);
            String[] dbLoginRecordCode=systems.getTableDataArrayNum(OpenMSApp.Database_A, "LoginRecord",1);
            String[] dbComputerName=systems.getTableDataArrayNum(OpenMSApp.Database_A, "LoginRecord",5);

            if(searchPara.isEmpty()==false)
            {
                String multipleSearch[]=searchPara.split(",");
                String queryPara="where (cLoginCode = '"+multipleSearch[0]+"') or (dLogin = '"+multipleSearch[0]+"') or (vComputerName = '"+multipleSearch[0]+"') ";

                for(int k=1;k<multipleSearch.length;k++)
                {
                    queryPara+="or (cLoginCode like '%"+multipleSearch[k]+"%') or (dLogin like '%"+multipleSearch[k]+"%') or (vComputerName like '%"+multipleSearch[k]+"%') ";
                }

                dbLoginCode=systems.getColumn(OpenMSApp.Database_A, "Select * from LoginRecord "+queryPara, 2);
                dbLoginTime=systems.getColumn(OpenMSApp.Database_A, "Select * from LoginRecord "+queryPara, 3);
                dbLogoutTime=systems.getColumn(OpenMSApp.Database_A, "Select * from LoginRecord "+queryPara, 4);
                dbLoginRecordCode=systems.getColumn(OpenMSApp.Database_A, "Select * from LoginRecord "+queryPara, 1);
                dbComputerName=systems.getColumn(OpenMSApp.Database_A, "Select * from LoginRecord "+queryPara, 5);
            }

        int x=0;
        int y=dbLoginRecordCode.length;

        tableObject=new Object[y][5];

        while(x<y)
        {
            if(dbLogoutTime[x]==null)
            {
                dbLogoutTime[x]="N/A";
            }
            String employeeID=systems.getValue(OpenMSApp.Database_A, "Select * from Login Where cLoginCode='"+dbLoginCode[x]+"'", "cEmployeeID");
            String employeeName=systems.getValue(OpenMSApp.Database_A,"Select * from Employee where cEmployeeID='"+employeeID+"'","vFirstName")+" "+
                    systems.getValue(OpenMSApp.Database_A,"Select * from Employee where cEmployeeID='"+employeeID+"'","vMiddleName")+" "+
                    systems.getValue(OpenMSApp.Database_A,"Select * from Employee where cEmployeeID='"+employeeID+"'","vlastName");
            tableObject[x][0]=dbLoginCode[x];
            tableObject[x][1]=employeeName.trim();
            tableObject[x][2]=dbLoginTime[x].trim();
            tableObject[x][3]=dbLogoutTime[x].trim();
            tableObject[x][4]=dbComputerName[x].trim();
            systems.freeHeap();
            x+=1;
            if(x==y){break;}
       }
        loginRecordTable.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "Login Code", "Employee Name", "Login Time", "Logout Time","Computer Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false,false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jRefreshButton;
    private javax.swing.JScrollPane loginRecordScrollPane;
    private javax.swing.JTable loginRecordTable;
    // End of variables declaration//GEN-END:variables

}
